//输入一组日期列表,返回距离当前时间最近的日期
function getRecentDate(dateList) {
    const timeArr = dateList;
    const timestamp = Date.now();
    let min;
    let index = 0;
    timeArr.forEach(function (item, i) {
        const itemStamp = new Date(item).getTime();
        const interVal = Math.abs(timestamp - itemStamp);
        if (min === void 0) {
            min = interVal
        } else {
            if (min > interVal) {
                min = interVal;
                index = i;
            }
        }
    })
    return dateList[index];
}

//根据输入的日期过滤出相关的数据
function getDataWithDate(data, date) {
    return (date === '') ? data : data.filter(item => item.date === date)
}

//根据输入学科名获取相关数据
function getDataWithSubject(data, subject) {
    return (subject === '') ? data : data.filter(item => item.subject === subject)
}

let grades = [
    {subject: '英语', date: '2015-10-4', grade: 77},
    {subject: '数学', date: '2015-10-4', grade: 99},
    {subject: '英语', date: '2015-10-4', grade: 65},
    {subject: '生物', date: '2015-10-4', grade: 93},
    {subject: '语文', date: '2015-10-1', grade: 95},
    {subject: '数学', date: '2015-10-4', grade: 100},
    {subject: '体育', date: '2015-10-4', grade: 93},
    {subject: '英语', date: '2015-10-3', grade: 64},
    {subject: '英语', date: '2015-10-1', grade: 91},
    {subject: '英语', date: '2015-10-1', grade: 89},
    {subject: '政治', date: '2015-10-1', grade: 80},
    {subject: '英语', date: '2015-10-1', grade: 51},
    {subject: '体育', date: '2015-10-2', grade: 65},
    {subject: '政治', date: '2015-10-3', grade: 74},
    {subject: '政治', date: '2015-10-4', grade: 67},
    {subject: '数学', date: '2015-10-2', grade: 81},
    {subject: '英语', date: '2015-10-2', grade: 82},
    {subject: '政治', date: '2015-10-2', grade: 89},
    {subject: '政治', date: '2015-10-4', grade: 51},
    {subject: '语文', date: '2015-10-3', grade: 100},
    {subject: '数学', date: '2015-10-4', grade: 66},
    {subject: '体育', date: '2015-10-2', grade: 68},
    {subject: '政治', date: '2015-10-2', grade: 58},
    {subject: '英语', date: '2015-10-1', grade: 56},
    {subject: '体育', date: '2015-10-2', grade: 86},
    {subject: '英语', date: '2015-10-3', grade: 74},
    {subject: '政治', date: '2015-10-4', grade: 75},
    {subject: '体育', date: '2015-10-2', grade: 62},
    {subject: '英语', date: '2015-10-1', grade: 61},
    {subject: '英语', date: '2015-10-2', grade: 71}
]
console.log(getDataWithDate(grades, '2015-10-3'))
export {getRecentDate, getDataWithDate, getDataWithSubject};



